import { Component, OnInit } from '@angular/core';
import {ExtendHttpService} from "../../../../common/extend-http.service";
import {EssService} from "../../ess.service";
import {ActivatedRoute, Router} from "@angular/router";
import {debounceTime} from "rxjs/operator/debounceTime";
import {Observable} from "rxjs/Observable";
import {TipService} from '../../../services/tip.service';

@Component({
  selector: 'app-inventory-add',
  templateUrl: './inventory-add.component.html',
  styleUrls: ['./inventory-add.component.css']
})
export class InventoryAddComponent implements OnInit {

  tipType: any = 'error';

  editInfo: any = {
    'ms.materiel_id': '',
    'ms.name': '选择',
    'ms.unit': '',
    'ms.num': '',
    'instockname': '',
    'price': '',
  };

  unitList: any = []; // 单位列表

  id: any = '';

  tips1 = '';
  tips2 = '';
  tips3 = '';
  tips4 = '';

  matList: any = []; // 物料列表
  showList: any = false; // 下拉搜索菜单是否出现
  matName: any = '选择'; // 选择的物料名称

  constructor(
    private ext: ExtendHttpService, public ess: EssService,
    private router: Router, private route: ActivatedRoute,
    private tips: TipService
  ) {
  }

  ngOnInit() {
    this.getMatList();

    // 取到会员卡id
    this.id = this.route.queryParams['value']['id'];
    // if (this.id) {
    //   this.getEditInfo(this.id);
    // }
  }

  // 删除单位
  deleteUnit(h) {
    this.editInfo['mt.unit'].splice(h, 1);
  }

  // 获取物料列表
  getMatList() {
    this.ext.get('/merchantApp/inventory/stock/getmateriel', {start: 1}).subscribe(res => {
      let info = res.json();
      if (info.code === '0') {
        this.matList = info.page;
      } else {
        this.ess.errTips(info);
      }
    });
  }

  // 是否显示下拉框
  isShowList(e) {
    e.stopPropagation();
    this.showList = !this.showList;
  }

  // 下拉框搜索
  searchKey(e, el) {
    let input = Observable.fromEvent(el, 'input');
    input.debounceTime(500)
      .subscribe(ev => {
        // console.log(el.value);
        this.matList.forEach(item => {
          if (item.name.indexOf(el.value) === -1) {
            item.hide = true;
          } else {
            item.hide = false;
          }
        });
      });
  }

  // 点击选择下拉框
  chooseRes(v) {
    this.editInfo['ms.name'] = v.name;
    this.editInfo['ms.materiel_id'] = v.id;
    this.unitList = JSON.parse(v.unit);
    this.editInfo['ms.unit'] = this.unitList[0].unit;
    this.showList = false;
  }

  // clear清除选择
  clearChoose() {
    this.editInfo['ms.name'] = '选择';
    this.editInfo['ms.materiel_id'] = '';
    this.showList = false;
  }

  // 验证
  validationMethods() {
    if (!this.editInfo['ms.materiel_id']) {
      this.tips1 = '请填选择物料';
      this.tipType = 'error';
      this.tips.change(true, this.tips1);
    } else {
      this.tips1 = '';
    }
    if (!this.editInfo['ms.unit']) {
      this.tips2 = '请选择单位';
      this.tipType = 'error';
      this.tips.change(true, this.tips2);
    } else {
      this.tips2 = '';
    }
    if (!this.editInfo['ms.num']) {
      this.tips3 = '请填写数量';
      this.tipType = 'error';
      this.tips.change(true, this.tips3);
    } else {
      this.tips3 = '';
    }
    if (!this.editInfo['price']) {
      this.tips4 = '请填写总价';
      this.tipType = 'error';
      this.tips.change(true, this.tips4);
    } else {
      this.tips4 = '';
    }

    if (!this.tips1 && !this.tips2 && !this.tips3 && !this.tips4) {
      return true;
    } else {
      return false;
    }
  }

  // 保存
  save() {
    if (!this.validationMethods()) {
      return;
    }

    let param = Object.assign({}, this.editInfo);
    let url = this.id === '0' ? '/merchantApp/inventory/stock/warehousing' : '/merchantApp/inventory/stock/extractStock';
    this.ext.post(url, param).subscribe(res => {
      this.handInfo(res.json());
    });
  }

  // 请求返回信息处理
  handInfo(info) {
    if (info.code === '0') {
      this.tipType = 'success';
      this.tips.change(true, info.message);
      setTimeout(() => {
        this.back();
      }, 500);
    } else {
      this.tipType = 'error';
      this.tips.change(true, this.ess.errTips(info));
    }
  }

  // 取消添加
  back() {
    this.router.navigateByUrl('/main/ess/inventory');
  }


}
